/**
 * 
 */
package nl.boektrust.btsoep.client;

import java.util.ArrayList;

import nl.boektrust.btsoep.shared.model.Contributor;
import nl.boektrust.btsoep.shared.model.ContributorRoleCode;
import nl.boektrust.btsoep.shared.model.Person;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author rao3
 *
 */
public  class EditBookContributorPanel extends EditBookPanel implements ClickHandler, ValueChangeHandler, KeyPressHandler, ChangeHandler {
	private Contributor contributor = new Contributor();
	private ListBox roleListBox = new ListBox();
	private TextBox nameOnBook = new TextBox();
	private ListBox personListBox = new ListBox();
	private VerticalPanel vPanel = new VerticalPanel();
	//private Label theLabel;
	private Button xButton = new Button("x");;
	private ArrayList<Person> personList;

	/**
	 * @return the personList
	 */
	public ArrayList<Person> getPersonList() {
		return personList;
	}




	/**
	 * @param personList the personList to set
	 */
	public void setPersonList(ArrayList<Person> personList) {
		this.personList = personList;
	}




	public EditBookContributorPanel(String label, boolean isInEditMode) {
		super(label, isInEditMode);
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.add(new Label("Naam: "));
		hPanel.add(nameOnBook);
		vPanel.add(hPanel);
		hPanel.add(new Label("Rol: "));
		hPanel.add(roleListBox);
		vPanel.add(hPanel);
		hPanel.add(new Label("Persoon:"));
		hPanel.add(personListBox);
		vPanel.add(hPanel);
		editPanel.add(vPanel);
		editPanel.add(xButton);
		xButton.addClickHandler(this);	
		if (isInEditMode == true) {
			setToEditMode();
		} else {
			setToDisplayMode();
		}
		populateRoleListBox(ContributorRoleCode.UNDEFINED);
		personListBox.addChangeHandler(this);
		roleListBox.addChangeHandler(this);
		nameOnBook.addValueChangeHandler(this);
	}
	


	
	public EditBookContributorPanel(boolean isInEditMode) {
		this("", isInEditMode);
	}

	public EditBookContributorPanel(String label) {
		this(label, false);
	}

	public EditBookContributorPanel() {
		this(false);
	}
	



	boolean hasInvalidData() {
		return false;
	}


	@Override
	public void setToDisplayMode() {
		super.setToDisplayMode();
		
		try {
			if (contributor != null && contributor.getNameOnBook() != null && contributor.getNameOnBook().length() > 0) {
				if (contributor.getPerson() == null) {
					theLabel.setText("\""+contributor.getNameOnBook()+"\" ("+ContributorRoleCode.stringValue(contributor.getRoleCode())+") ");

				} else {
					theLabel.setText("\""+contributor.getNameOnBook()+"\" ("+ContributorRoleCode.stringValue(contributor.getRoleCode())+") - "+contributor.getPerson().getName());
				}


			} else {
				theLabel.setText("Klik om te veranderen");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override 
	public void setToEditMode() {
		super.setToEditMode();
		if (contributor != null) {
			if (contributor.getNameOnBook() != null) nameOnBook.setText(contributor.getNameOnBook());
			for (int i=0; i<roleListBox.getItemCount(); i++) {
				if (roleListBox.getItemText(i).equals(ContributorRoleCode.stringValue(contributor.getRoleCode()))) {
					roleListBox.setSelectedIndex(i);
				}
			}
			if (contributor.getPerson() != null) {
				for (int i=0; i<personListBox.getItemCount(); i++) {
					if (personListBox.getValue(i).equals(contributor.getPerson().getId())) {
						personListBox.setSelectedIndex(i);
					}
				}
			}
		}
	}

	public void onClick(ClickEvent event) {
		if (event.getSource() ==theLabel) {		
			this.setToEditMode();
		} else if (event.getSource() == xButton) {
			setInEditMode(false);
			this.setToDisplayMode();
		}
	}

	public void setValue() {
		contributor.setNameOnBook(nameOnBook.getText());
		contributor.setRoleCode(ContributorRoleCode.parseStringIntoContributorRoleCode(roleListBox.getItemText(roleListBox.getSelectedIndex())));
		// find the person that matches the selected person in the listbox
		Long id = new Long(personListBox.getValue(personListBox.getSelectedIndex()));
		for (Person p : personList) {
			if (p.getId().equals(id)) {
				contributor.setPerson(p);
			}
		}

	}
	

	
	public void onValueChange(ValueChangeEvent event) {
		setValue();
	}

	public void onKeyPress(KeyPressEvent event) {

	}


	
	public void populatePersonListBox() {
		if (personList != null) {
			for (Person p : personList) {
				personListBox.addItem(p.getName(), new Long(p.getId()).toString());
			}
		} else {
			System.err.println("Cannot populate person listbox: personList is null");
		}
	}
	
	void removeListboxItems(ListBox theListBox) {
		for (int i = theListBox.getItemCount()-1; i>=0; i--) theListBox.removeItem(i);
	}
	
	public void populateRoleListBox(ContributorRoleCode arg) {
		try {
			removeListboxItems(roleListBox);
			for (ContributorRoleCode c : ContributorRoleCode.values()) {
				roleListBox.addItem(ContributorRoleCode.stringValue(c));
				if (c == arg) {
					roleListBox.setSelectedIndex(roleListBox.getItemCount()-1);
					//valueChanged();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}




	public void onChange(ChangeEvent event) {
		setValue();
	}
	/**
	 * @param contributor the contributor to set
	 */
	public void setContributor(Contributor contributor) {
		this.contributor = contributor;
	}
	
	public Contributor getContributor() {
		return this.contributor;
	}


}
